3. Operators

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

An operator is a character that represents an action. It tells the compiler/interpreter to perform specific mathematical, relational or logical operation and produce final result. PowerShell interprets in a specific way and categorizes accordingly like arithmetic operators perform operations primarily on numbers, but they also affect strings and other data types. Along with the basic operators, PowerShell has a number of operators that save time and coding effort (eg: -like, -match, -replace, etc).

3.1: Comparison Operators

PowerShell comparison operators are comprised of a leading dash (-) followed by a name (eq for equal, gt for

greater than, etc...).

Names can be preceded by special characters to modify the behavior of the operator:

1
2
i # Case-Insensitive Explicit (-ieq)
c # Case-Sensitive Explicit (-ceq)

Case-Insensitive is the default if not specified, ("a" -eq "A") same as ("a" -ieq "A").

Simple comparison operators:

1
2
3
4
5
6
2 -eq 2 # Equal to (==)
2 -ne 4 # Not equal to (!=)
5 -gt 2 # Greater-than (>)
5 -ge 5 # Greater-than or equal to (>=)
5 -lt 10 # Less-than (<)
5 -le 5 # Less-than or equal to (<=)

String comparison operators:

1
2
3
4
"MyString" -like "*String" # Match using the wildcard character (*)
"MyString" -notlike "Other*" # Does not match using the wildcard character (*)
"MyString" -match "$String^" # Matches a string using regular expressions
"MyString" -notmatch "$Other^" # Does not match a string using regular expressions

Collection comparison operators:

1
2
3
4
5
6
7
8
"abc", "def" -contains "def" # Returns true when the value (right) is present
# in the array (left)
"abc", "def" -notcontains "123" # Returns true when the value (right) is not present
# in the array (left)
"def" - in "abc", "def" # Returns true when the value (left) is present
# in the array (right)
"123" - notin "abc", "def" # Returns true when the value (left) is not present
# in the array (right)

3.2: Arithmetic Operators

1
2
3
4
5
6
1 + 2 # Addition
1 - 2 # Subtraction
- 1 # Set negative value
1 * 2 # Multiplication
1 / 2 # Division
1 % 2 # Modulus
1
2
100 - shl 2 # Bitwise Shift-left
100 - shr 1 # Bitwise Shift-right

3.3: Assignment Operators

Simple arithmetic:

1
2
3
4
5
6
7
$var = 1 # Assignment. Sets the value of a variable to the specified value
$var += 2 # Addition. Increases the value of a variable by the specified value
$var -= 1 # Subtraction. Decreases the value of a variable by the specified value
$var *= 2 # Multiplication. Multiplies the value of a variable by the specified value
$var /= 2 # Division. Divides the value of a variable by the specified value
$var %= 2 # Modulus. Divides the value of a variable by the specified value and then
# assigns the remainder (modulus) to the variable

Increment and decrement:

1
2
$var++ # Increases the value of a variable, assignable property, or array element by 1
$var-- # Decreases the value of a variable, assignable property, or array element by 1

3.4: Redirection Operators

Success output stream:

1
2
3
cmdlet > file # Send success output to file, overwriting existing content
cmdlet >> file # Send success output to file, appending to existing content
cmdlet 1 >& 2 # Send success and error output to error stream

Error output stream:

1
2
3
cmdlet 2 > file # Send error output to file, overwriting existing content
cmdlet 2 >> file # Send error output to file, appending to existing content
cmdlet 2 >& 1 # Send success and error output to success output stream

Warning output stream: (PowerShell 3.0+)

1
2
3
cmdlet 3 > file # Send warning output to file, overwriting existing content
cmdlet 3 >> file # Send warning output to file, appending to existing content
cmdlet 3 >& 1 # Send success and warning output to success output stream

Verbose output stream: (PowerShell 3.0+)

1
2
3
cmdlet 4 > file # Send verbose output to file, overwriting existing content
cmdlet 4 >> file # Send verbose output to file, appending to existing content
cmdlet 4 >& 1 # Send success and verbose output to success output stream

Debug output stream: (PowerShell 3.0+)

1
2
3
cmdlet 5 > file # Send debug output to file, overwriting existing content
cmdlet 5 >> file # Send debug output to file, appending to existing content
cmdlet 5 >& 1 # Send success and debug output to success output stream

Information output stream: (PowerShell 5.0+)

1
2
cmdlet 6 > file # Send information output to file, overwriting existing content
cmdlet 6 >> file # Send information output to file, appending to existing content
1
cmdlet 6 >& 1 # Send success and information output to success output stream

All output streams:

1
2
3
cmdlet *> file # Send all output streams to file, overwriting existing content
cmdlet *>> file # Send all output streams to file, appending to existing content
cmdlet *>& 1 # Send all output streams to success output stream

Differences to the pipe operator (|)

Redirection operators only redirect streams to files or streams to streams. The pipe operator pumps an object down the pipeline to a cmdlet or the output. How the pipeline works differs in general from how redirection works and can be read on Working with the PowerShell pipeline

3.5: Mixing operand types, the type of the left operand dictates the behavior

For Addition

1
2
3
4
"4" + 2 # Gives "42"
4 + "2" # Gives 6
1 , 2 , 3 + "Hello" # Gives 1,2,3,"Hello"
"Hello" + 1 , 2 , 3 # Gives "Hello1 2 3"

For Multiplication

1
2
3
4
"3" * 2 # Gives "33"
2 * "3" # Gives 6
1 , 2 , 3 * 2 # Gives 1,2,3,1,2,
2 * 1 , 2 , 3 # Gives an error op_Multiply is missing

The impact may have hidden consequences on comparison operators:

1
2
3
4
$a = Read-Host "Enter a number"
Enter a number : 33
$a -gt 5
False

3.6: Logical Operators

1
2
-and # Logical and
-or # Logical or
  • xor # Logical exclusive or -not # Logical not ! # Logical not

3.7: String Manipulation Operators

Replace operator:

The -replace operator replaces a pattern in an input value using a regular expression. This operator uses two arguments (separated by a comma): a regular expression pattern and its replacement value (which is optional and an empty string by default).

1
"The rain in Seattle" -replace 'rain','hail' #Returns: The hail in Seattle
1
"[email protected]" -replace '^[\w]+@(.+)', '$1' #Returns: contoso.com

Split and Join operators:

The -split operator splits a string into an array of sub-strings.

1
"A B C" - split " " #Returns an array string collection object containing A,B and C.

The -join operator joins an array of strings into a single string.

1
"E","F","G" - join ":" #Returns a single string: E:F:G